// Budget Management Application
class BudgetManager {
  List<Map<String, double>> incomeList = [];
  List<Map<String, double>> expenseList = [];

  void addIncome(String desc, double amount) {
    if (amount > 0) {
      incomeList.add({desc: amount});
      print("\nIncome added successfully!");
    } else {
      print("\nInvalid amount!");
    }
  }

  void addExpense(String desc, double amount) {
    if (amount > 0) {
      expenseList.add({desc: amount});
      print("\nExpense added successfully!");
    } else {
      print("\nInvalid amount!");
    }
  }

  void viewSummary() {
    double totalIncome = incomeList.fold(0, (sum, item) => sum + item.values.first);
    double totalExpenses = expenseList.fold(0, (sum, item) => sum + item.values.first);
    double netProfit = totalIncome - totalExpenses;
    double tax = netProfit > 0 ? netProfit * 0.15 : 0;

    print("\nTotal Income: \$${totalIncome.toStringAsFixed(2)}");
    print("Total Expenses: \$${totalExpenses.toStringAsFixed(2)}");
    print("${netProfit >= 0 ? 'Net Profit' : 'Net Loss'}: \$${netProfit.toStringAsFixed(2)}");
    if (tax > 0) print("Tax Due: \$${tax.toStringAsFixed(2)}");
  }
}

void mainBudget() {
  BudgetManager budget = BudgetManager();
  while (true) {
    print("\n1. Add Income\n2. Add Expense\n3. View Summary\n4. Exit");
    stdout.write("Choose an option: ");
    String? choice = stdin.readLineSync();
    
    switch (choice) {
      case '1':
        stdout.write("Enter income description: ");
        String desc = stdin.readLineSync()!;
        stdout.write("Enter income amount: ");
        double? amount = double.tryParse(stdin.readLineSync()!);
        if (amount != null) budget.addIncome(desc, amount);
        break;
      case '2':
        stdout.write("Enter expense description: ");
        String desc = stdin.readLineSync()!;
        stdout.write("Enter expense amount: ");
        double? amount = double.tryParse(stdin.readLineSync()!);
        if (amount != null) budget.addExpense(desc, amount);
        break;
      case '3':
        budget.viewSummary();
        break;
      case '4':
        print("\nProgram exited.");
        return;
      default:
        print("\nInvalid option!");
    }
  }
}
